XMPQT = {}
XLSetGlobal("XMPQT", XMPQT)
XMPQT.AssertSignalString = "{8D19E96F-29E4-41c5-B889-AA38F6F4345A}xmpqt_"
XMPQT.API = nil

local XMPQuickTest_Factory = XLGetObject("XMPQuickTest.Factory")
if nil ~= XMPQuickTest_Factory then
		local XMPQuickTest = XMPQuickTest_Factory:CreateInstance()
		if nil~= XMPQuickTest then
			XMPQT.API = XMPQuickTest
		end
end

function XMPQT.LOG(...)--打日志
	local enable = XMPQT.API:Call("TSInfo")
 	if enable then	
		local tback = debug.traceback("", 2)
		local s = string.gsub(tback, "\n", " < ") 
		s =string.sub(s,1, 512)
		XMPQT.API:Call("TSInfo", "  ::: (luatraceback : "..s..")", ...)
	end
end

--中断所有用例
function XMPQT.Assert(actual,info)
	local actualtype = type(actual)
	if actualtype ~= "boolean" then
		XLMessageBox("类型必须为bool类型")
	end
	
	if actual then
		return
	end
	
	--SetOnceTimer(
	--function()
	assert(false,XMPQT.AssertSignalString.."__assert__"..tostring(actual)..":\n"..tostring(info))
	--end,300)
end

--返回当前用例，继续执行下一个用例
function XMPQT.Continue(actual,info)

	local actualtype = type(actual)
	if actualtype ~= "boolean" then
		XLMessageBox("类型必须为bool类型")
	end
	
	SetOnceTimer(
	function()
		assert(false,XMPQT.AssertSignalString..tostring(actual)..":"..tostring(info))
	end,300)
end

function XMPQT.XMPCommand(command)
	local ret = XMPQT.API:Call("XMPCommand",command)
	return ret
end

-----------------------------------------------------------------------------------------
XMPQT.EventSource = {}
function XMPQT.EventSource:new(o)
	o = o or {listeners={}, listeners2={}}
	setmetatable(o,self)
	self.__index = self
	return o;
end
function XMPQT.EventSource:addEventListener(event,notify,l, index)
													--index是命名，值必须先定是谁
	--XLMessageBox("event = " .. event .. " index : " .. (index and index or "nil"))
	if(not self.listeners[event]) then
		self.listeners[event]={}
	end
	if(not self.listeners2[event]) then
		self.listeners2[event]={}
	end
	if type(index) == "number" then
		--XMP.Helper.Assert(not self.listeners2[event][index], "重复订阅 " .. index)
		self.listeners2[event][index] = {["notify"] = notify, ["listener"]=l} --拥有优先级的订阅者
	else
		table.insert(self.listeners[event], {["notify"] = notify, ["listener"]=l})		
	end		
	--self.listeners[event][l]=notify
	XMPQT.LOG("self : " .. tostring(self) .. ", event .. " .. event .. " size : " .. #self.listeners[event] .. " size2 : " ..#self.listeners2[event])
end

function XMPQT.EventSource:dispatchEvent(event, ...)
	if self.listeners2[event] then
		local listenert = self.listeners2[event]
		local keysortt = {}
		table.foreach(listenert, function(k, v)
			table.insert(keysortt, k)
		end)
		table.sort(keysortt)
		for i = 1, #keysortt do
			if listenert[keysortt[i]] and listenert[keysortt[i]]["notify"] then
				local retTable = {listenert[keysortt[i]]["notify"](listenert[keysortt[i]]["listener"],event, ...)}
				if retTable and retTable[1] then
					return unpack(retTable) --拥有优先级的订阅者，一返回接下来的订阅者将收不到信息
				end
			end
		end
	end
	if self.listeners[event] then
		local i = 0
		local function continue(t)
			if i >= #t then
				return false
			end
			i = i + 1
			return true
		end
		while continue(self.listeners[event]) do
			if self.listeners[event][i] and self.listeners[event][i]["notify"] then
				local retTable = {self.listeners[event][i]["notify"]( self.listeners[event][i]["listener"],event, ...)}
				if retTable and retTable[1] then
					return unpack(retTable)
				end
			end
		end
	end
end

function XMPQT.EventSource:detachEventListener(event,notify,l)
	if  self.listeners and self.listeners[event] then
		local i = 0
		local function continue(t)
			if i >= #t then
				return false
			end
			i = i + 1
			return true
		end
		while continue(self.listeners[event]) do
			if self.listeners[event][i] and self.listeners[event][i]["notify"] 
				and self.listeners[event][i]["notify"]==notify
				and self.listeners[event][i]["listener"] == l then
				table.remove(self.listeners[event][i], i)			
			end
		end
	end
	
	if self.listeners2 and self.listeners2[event] then
		local listenert = self.listeners2[event]
		table.foreach(listenert, function(k, v)
			 if listenert[k].notify == notify and listenert[k].listener == l then
				listenert[k] = nil
			 end
		end)
	end
end
------------------------------------------------

XMPQT.QTEvent = XMPQT.EventSource:new()

XMPQT.mainwnd = {}





